package view.viewComponents 
{
	import com.greensock.easing.Linear;
	import com.greensock.TweenLite;
	import controller.EventBus;
	import controller.GameViewControllerEvent;
	import controller.ZeldaComponentEvent;
	import controller.ZeldaModelEvent;
	import model.data.MovementDirections;
	import mx.core.FlexSprite;
	import org.flixel.FlxG;
	import org.flixel.FlxSprite;
	import view.viewControllers.GameViewController;
	
	/**
	 * ...
	 * @author Gaz Williams
	 * 
	 * the display component or sprite which represents Hyro Protagonist in this game...
	 * 
	 */
	
	public class Zelda extends FlxSprite 
	{
		[Embed(source = "../../../embedded/zelda_spritesheet.png")]
		private var zeldaTexture:Class;
		
		[Embed(source = "../../../embedded/spacebar.png")]
		private var spacebarTexture:Class;
		
		private var tileSize:uint;
		private var bus:EventBus;
		
		public function Zelda(tileSize:uint, spawnColumn:int, spawnRow:int) 
		{
			super(spawnColumn * tileSize, spawnRow * tileSize);
			
			this.loadGraphic(zeldaTexture, true, false, 32, 32, false);
			
			this.addAnimation("walk up", 	[0, 1], 2, true);
			this.addAnimation("walk right", [2, 3], 2, true);
			this.addAnimation("walk down",	[4, 5], 2, true);
			this.addAnimation("walk left", 	[6, 7], 2, true);
			
			this.addAnimation("idle", 		[8], 1, true);
			
			this.addAnimation("walk up with space", 	[9, 10], 2,  true);
			this.addAnimation("walk right with space",	[11, 12], 2, true);
			this.addAnimation("walk down with space",	[13, 14], 2, true);
			this.addAnimation("walk left with space", 	[15, 16], 2, true);
			
			this.tileSize = tileSize;
			
			//listen for updates from model
			bus = EventBus.getInstance();
			bus.addEventListener(ZeldaModelEvent.POSITION_UPDATED, handlePositionUpdate);
			bus.addEventListener(GameViewControllerEvent.PLAYER_HIT_NPC, handleNPCCollision);
			
			//bus.addEventListener(GameViewControllerEvent.PLAYER_INTERACTED_WITH_NPC, handleNPCInteraction);
			//bus.addEventListener(GameViewControllerEvent.PLAYER_INTERACTION_COMPLETED, handlePlayerInteractionCompleted);
		}
		
		override public function destroy():void 
		{
			bus.removeEventListener(ZeldaModelEvent.POSITION_UPDATED, handlePositionUpdate);
			bus.removeEventListener(GameViewControllerEvent.PLAYER_HIT_NPC, handleNPCCollision);
			
			//bus.removeEventListener(GameViewControllerEvent.PLAYER_INTERACTED_WITH_NPC, handleNPCInteraction);
			//bus.removeEventListener(GameViewControllerEvent.PLAYER_INTERACTION_COMPLETED, handlePlayerInteractionCompleted);
			
			super.destroy();
		}
		
		private function handlePositionUpdate(e:ZeldaModelEvent):void 
		{
			
			//trace("zelda receiving model update", e.data.column, e.data.row);
			
			var facing:MovementDirections = e.data.facing as MovementDirections;
			
			switch (facing.type) 
			{
				case MovementDirections.UP:
					play("walk up");
				break;
				case MovementDirections.DOWN:
					play("walk down");
				break;
				case MovementDirections.LEFT:
					play("walk left");
				break;
				case MovementDirections.RIGHT:
					play("walk right");
				break;
				default:
				case MovementDirections.IDLE: //this will change
					play("idle");
				break;
			}
			
			//target column and row
			var targetX:int = int(e.data.column) * tileSize;
			var targetY:int = int(e.data.row) 	* tileSize;
			
			TweenLite.to( this, 1, {  x:targetX, y:targetY, onComplete:this.notifyMoveComplete, ease:Linear.easeNone } );
			
		}
		
		private function notifyMoveComplete():void 
		{
			bus.dispatchEvent(new ZeldaComponentEvent(ZeldaComponentEvent.MOVE_COMPLETED));
		}
		
		private function handleNPCCollision(e:GameViewControllerEvent):void 
		{
			displaySpaceBar();
		}
		
		/*
		//move beyond collision tile
		private function handleNPCInteraction(e:GameViewControllerEvent):void 
		{
			TweenLite.killTweensOf(this);
		}
		
		private function handlePlayerInteractionCompleted(e:GameViewControllerEvent):void 
		{
			//TweenLite.killTweensOf(this, true, { onComplete:this.notifyMoveComplete } );
			//bus.dispatchEvent(new ZeldaComponentEvent(ZeldaComponentEvent.MOVE_COMPLETED));
		}
		*/
		
		private function displaySpaceBar():void 
		{
			switch (_curAnim.name) 
			{
				case "walk up":
					play("walk up with space");
				break;
				case "walk down":
					play("walk down with space");
				break;
				case "walk left":
					play("walk left with space");
				break;
				case "walk right":
					play("walk right with space");
				break;
				default:
			}
		}
		
	}

}